package com.newzqxq.koalamoney.presenter;

import com.newzqxq.koalamoney.common.App;
import com.newzqxq.koalamoney.common.SuperPresenter;
import com.newzqxq.koalamoney.entity.RegisterBean;
import com.newzqxq.koalamoney.model.LoginModel;
import com.newzqxq.koalamoney.net.Api;
import com.newzqxq.koalamoney.net.DaggerModelComponent;
import com.newzqxq.koalamoney.view.ILoginView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

/**
 * Created by ZQXQ-Developer on 2017/7/25.
 */

public class LoginPresenter extends SuperPresenter {
    private ILoginView iLoginView;

    @Inject
    LoginModel model;

    public LoginPresenter(ILoginView iLoginView) {
        this.iLoginView = iLoginView;
        setupDagger2();
    }

    private void setupDagger2() {
        DaggerModelComponent.builder().appComponent(App.getInstance().getAppComponent()).build().inject(this);
    }


    public void login(String phone, String login_pwd) {
        model.loging(phone, login_pwd);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void login(RespEvent<RegisterBean.DataBean> event) {
        if (event.getUrl().equals(Api.login)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    iLoginView.onLoginResult(event.getT());
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if (event.isAlert()) {
                        iLoginView.onShowToast(event.getExtra());
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    if (event.isAlert()) {
                        iLoginView.onShowToast(event.getExtra());
                    }
                    break;
            }
        }
    }
}
